package com.ruoyi.iot.service.impl;

import com.ruoyi.common.constant.CacheConstants;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.iot.domain.IotDevice;
import com.ruoyi.iot.domain.IotProductAttributeModel;
import com.ruoyi.iot.domain.IotProductConfig;
import com.ruoyi.iot.domain.IotRulesTriggerTerm;
import com.ruoyi.iot.mapper.IotRulesTriggerTermMapper;
import com.ruoyi.iot.service.IIotRulesTriggerTermService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 规则触发项Service业务层处理
 *
 * @author ruoyi
 * @date 2025-03-28
 */
@Service
public class IotRulesTriggerTermServiceImpl implements IIotRulesTriggerTermService {
    @Autowired
    private IotRulesTriggerTermMapper iotRulesTriggerTermMapper;
    @Autowired
    private RedisCache redisCache;
    /**
     * 查询规则触发项
     *
     * @param termId 规则触发项主键
     * @return 规则触发项
     */
    @Override
    public IotRulesTriggerTerm selectIotRulesTriggerTermByTermId(Long termId) {
        return iotRulesTriggerTermMapper.selectIotRulesTriggerTermByTermId(termId);
    }

    /**
     * 查询规则触发项列表
     *
     * @param iotRulesTriggerTerm 规则触发项
     * @return 规则触发项
     */
    @Override
    public List<IotRulesTriggerTerm> selectIotRulesTriggerTermList(IotRulesTriggerTerm iotRulesTriggerTerm) {

        List<IotRulesTriggerTerm> list = iotRulesTriggerTermMapper.selectIotRulesTriggerTermList(iotRulesTriggerTerm);
        for (IotRulesTriggerTerm triggerTerm : list) {
            IotProductConfig productConfig = redisCache.getCacheObject(CacheConstants.PRODUCT_KEY + triggerTerm.getProductId());
            triggerTerm.setProductName(productConfig.getProductName());
            IotDevice device =redisCache.getCacheObject(CacheConstants.DEVICE_KEY + triggerTerm.getDeviceId());
            triggerTerm.setDeviceName(device.getDeviceName());
            IotProductAttributeModel productAttributeModel = redisCache.getCacheObject(CacheConstants.ATTRIBUTE_KEY + triggerTerm.getAttributeId());
            triggerTerm.setAttributeName(productAttributeModel.getAttributeName());
        }
        return list;
    }

    /**
     * 新增规则触发项
     *
     * @param iotRulesTriggerTerm 规则触发项
     * @return 结果
     */
    @Override
    public int insertIotRulesTriggerTerm(IotRulesTriggerTerm iotRulesTriggerTerm) {
        iotRulesTriggerTerm.setCreateTime(DateUtils.getNowDate());
        return iotRulesTriggerTermMapper.insertIotRulesTriggerTerm(iotRulesTriggerTerm);
    }

    /**
     * 修改规则触发项
     *
     * @param iotRulesTriggerTerm 规则触发项
     * @return 结果
     */
    @Override
    public int updateIotRulesTriggerTerm(IotRulesTriggerTerm iotRulesTriggerTerm) {
        iotRulesTriggerTerm.setUpdateTime(DateUtils.getNowDate());
        return iotRulesTriggerTermMapper.updateIotRulesTriggerTerm(iotRulesTriggerTerm);
    }

    /**
     * 批量删除规则触发项
     *
     * @param termIds 需要删除的规则触发项主键
     * @return 结果
     */
    @Override
    public int deleteIotRulesTriggerTermByTermIds(Long[] termIds) {
        return iotRulesTriggerTermMapper.deleteIotRulesTriggerTermByTermIds(termIds);
    }

    /**
     * 删除规则触发项信息
     *
     * @param termId 规则触发项主键
     * @return 结果
     */
    @Override
    public int deleteIotRulesTriggerTermByTermId(Long termId) {
        return iotRulesTriggerTermMapper.deleteIotRulesTriggerTermByTermId(termId);
    }
}
